package com.wwj.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.springboot.annotation.LogAnnotation;
import com.wwj.springboot.entity.CmfzUser;
import com.wwj.springboot.mapper.CmfzUserMapper;
import com.wwj.springboot.service.CmfzUserService;
import com.wwj.springboot.utils.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author wwj
 */
@Service
@Transactional
public class CmfzUserServiceImpl extends ServiceImpl<CmfzUserMapper, CmfzUser> implements CmfzUserService{

    @Autowired
    private CmfzUserMapper cmfzUserMapper;

    @Override
    @Transactional(readOnly = true)
    public Map selectByPage(int page, int limit,String userName,String userTelphone) {

        int count = cmfzUserMapper.selectCounts(userName,userTelphone);
        int offset=(page-1)*limit;
        List<CmfzUser> list = cmfzUserMapper.selectByPage(offset,limit,userName,userTelphone);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;

    }



    @LogAnnotation(type="update",value="修改了用户")
    @Override
    public Map updateUser(CmfzUser user) {
        Map map = new HashMap();
        try {
            int i = baseMapper.updateById(user);
            map.put("update",true);
        } catch (Exception exception) {
            map.put("update",false);
            exception.printStackTrace();
        }
        return map;
    }
    @LogAnnotation(type="selectOne",value="查看单个用户")
    @Override
    @Transactional(readOnly = true)
    public CmfzUser selectOne(int id) {
        QueryWrapper<CmfzUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        return baseMapper.selectOne(wrapper);
    }

    @LogAnnotation(type="insert",value="添加了一个用户")
    @Override
    public Map addUser(CmfzUser user) {
        Map map = new HashMap();
        try {
            cmfzUserMapper.insert(user);
            map.put("insert",true);
        } catch (Exception exception) {
            map.put("insert",false);
            exception.printStackTrace();
        }
        return map;
    }
    @LogAnnotation(type="delete",value="删除数了一个用户")
    @Override
    public Map deleteUser(int id) {
        Map map = new HashMap();
        try {
            int i = baseMapper.deleteById(id);
            map.put("delete",true);
        } catch (Exception exception) {
            map.put("delete",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    public Map addUserList(List<CmfzUser> userList) {
        Map map = new HashMap();
        try {
            cmfzUserMapper.addUserList(userList);
            map.put("insert",true);
        } catch (Exception exception) {
            map.put("insert",false);
            exception.printStackTrace();
        }
        return map;
    }


    @Override
    @Transactional(readOnly = true)
    public int[] selectCount() {
        QueryWrapper<CmfzUser> wrapperMan = new QueryWrapper<>();
        wrapperMan.eq("user_sex","男");

        QueryWrapper<CmfzUser> wrapperWoman = new QueryWrapper<>();
        wrapperWoman.eq("user_sex","女");

        int  man = cmfzUserMapper.selectCount(wrapperMan);
        int woman =cmfzUserMapper.selectCount(wrapperWoman);
        int[] Array = {man,woman};
        return Array;
    }

    //@AddCacheAnnotation
    @Override
    @Transactional(readOnly = true)
    public List<Map> selectBySexCount() {
        return cmfzUserMapper.selectBySexCount();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Map> selectByProvinceCount() {
        return cmfzUserMapper.selectByProvinceCount();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Map> selectByWeekCount() {
        return cmfzUserMapper.selectByWeekCount();
    }



    /**
     * 一个方法中查询多个图表数据
     * @return
     */
    @Override
    public Map selectAllCount() {
        Map map = new HashMap();

        map.put("sex",selectBySexCount());

        map.put("week",selectByWeekCount());
        map.put("province",selectByProvinceCount());
        return map;
    }



    /**
     * 多线程
     */
    @Override
    public Map selectAllCountByThread() throws InterruptedException {
        Map map = new HashMap();
//        1.创建计数器
        CountDownLatch latch = new CountDownLatch(3);

//        2.通过线程池执行三个任务
        ThreadPoolExecutor executor = ThreadPollUtil.executor;

        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("sex",selectBySexCount());
//                计数器减一
                latch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    map.put("week",selectByWeekCount());
                }catch (Exception e){

                }finally {
                    latch.countDown();
                }

            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("province",selectByProvinceCount());
                latch.countDown();
            }
        });

//        3.让主线程等待
        latch.await();

        return map;
    }
}
